import ollama
import pdfplumber
from openai import OpenAI

ai_text = '''
               {
                 "选择题": [
                   {
                     "题号": "1",
                     "题目": "12 + 15 = ?",
                     "选项": {
                       "A": "27",
                       "B": "26",
                       "C": "25",
                       "D": "28"
                     },
                     "正确答案": "A"
                   },
                   {
                     "题号": "2",
                     "题目": "25 - 10 = ?",
                     "选项": {
                       "A": "15",
                       "B": "20",
                       "C": "10",
                       "D": "5"
                     },
                     "正确答案": "A"
                   }
                 ],
                 "多选题": [
                   {
                     "题号": "1",
                     "题目": "在100以内，哪些算式结果小于15？",
                     "选项": {
                       "A": "9 + 5",
                       "B": "10 + 4",
                       "C": "8 + 7",
                       "D": "12 + 2"
                     },
                     "正确答案": "A,B,D"
                   },
                   {
                     "题号": "2",
                     "题目": "在100以内，哪些算式结果大于15？",
                     "选项": {
                       "A": "10 + 5",
                       "B": "12 + 3",
                       "C": "14 + 2",
                       "D": "16 + 1"
                     },
                     "正确答案": "C,D"
                   }
                 ],
                 "填空题": [
                   {
                     "题号": "1",
                     "题目": "____ + 12 = 25",
                     "正确答案": "13"
                   },
                   {
                     "题号": "2",
                     "题目": "28 - ____ = 10",
                     "正确答案": "18"
                   }
                 ],
                 "简答题": [
                   {
                     "题号": "1",
                     "题目": "100以内加法的口算方法是什么？",
                     "正确答案": "先算十位，再算个位，或者用凑十法。"
                   },
                   {
                     "题号": "2",
                     "题目": "减法在加法中的作用是什么？",
                     "正确答案": "减法是加法的逆运算，可以用来求已知和减去一个加数，得到另一个加数。"
                   }
                 ]
               }
               '''
def pdf_questions(pdf_file,single_choice,multiple_choice,fill_blank,short_answer):
    '''
    pdf_file: pdf文件
    single_choice: 选择题数量
    multiple_choice: 多选题数量
    fill_blank: 填空题数量
    short_answer: 简答题数量
    '''
    text2 = """出题要求：
            1. 选择题提供4个选项，选项用字母A、B、C、D标识
            2. 填空题要考察关键知识点
            3.根据pdf文件中的章节进行分段，按章节提取知识点
            4. 必须严格按照以下JSON格式输出，字段不可缺失或更改：
            {
                "选择题": [
                    {   "章节":
                        "题号": "1",
                        "题目": "具体题目内容",
                        "选项": {
                            "A": "选项A内容",
                            "B": "选项B内容",
                            "C": "选项C内容",
                            "D": "选项D内容"
                        },
                        "正确答案":  "A,B,C,D",
                    },
                ],
                "多选题": [
                    {   "章节":
                        "题号": "1",
                        "题目": "具体题目内容",
                        "选项": {
                            "A": "选项A内容",
                            "B": "选项B内容",
                            "C": "选项C内容",
                            "D": "选项D内容"
                        },
                        "正确答案":  "A,B,C,D",
                    },
                ],
                "填空题": [
                    {   "章节":
                        "题号": "1",
                        "题目": "具体题目内容（需填空处用____代替）",
                        "正确答案": "答案内容",
                    },
                ],
                "简答题": [
                    {   "章节":
                        "题号": "1",
                        "题目": "具体题目内容",
                        "正确答案": "答案内容",
                    },
                ]
            }
            """
    text3 = (
            '出题内容:选择题' + str(single_choice) + '道;' +
            '多选题' + str(multiple_choice) + '道;' +
            '填空题' + str(fill_blank) + '道;' +
            '简答题' + str(short_answer) + '道;\n\n' +
            '格式要求:严格按照指定JSON格式提供正确答案，不要添加任何额外说明文字，输出时仅可生成JSON字符串，不可输出其他内容,不要使用```json标记。'
    )

    print('开始解析pdf文件')
    pdf_text = ''
    with pdfplumber.open(pdf_file) as pdf:
        for page in pdf.pages:
            pdf_text += page.extract_text()
    print('pdf文件解析完成')


   # ''' client = OpenAI(
   #      base_url="http://127.0.0.1:11434/v1",
   #      api_key="<KEY>",
   #  )
   #
   #  print('开始ai出题')
   #  response = client.chat.completions.create(
   #       model='qwen3:4b',
   #      messages=[
   #           {'role': 'system', 'content': text2 + text3},
   #           {'role': 'user', 'content': pdf_text}
   #       ]
   #   )'''

    client = OpenAI(
        base_url="https://open.bigmodel.cn/api/paas/v4",
        api_key="cde7f8debacb414c84a352b4b8c46d65.ASqJkdauVhaqE4td",
    )

    print('开始ai出题')
    response = client.chat.completions.create(
        model='glm-4.6',
        messages=[
            {'role': 'system', 'content': text2 + text3},
            {'role': 'user', 'content': pdf_text}
        ]
    )

    ai_response = response.choices[0].message.content
    # ai_response=ai_text
    print('ai出题完成')
    # 清理可能的 Markdown 代码块标记
    if ai_response.startswith("```json"):
        ai_response = ai_response[7:]
    if ai_response.endswith("```"):
        ai_response = ai_response[:-3]
    ai_response = ai_response.strip()
    print(ai_response)
    return ai_response